package com.srd.phone.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RadioGroup;

import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengrongdai.srd.R;
import com.srd.phone.activity.TenderInvestActivity;
import com.srd.phone.adapter.TabTenderListLicaiAdapter;
import com.srd.phone.bean.TenderInfo;
import com.srd.phone.mvp.presenters.BasePresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.LogUtils;
import com.srd.phone.utils.views.FlowRadioGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主界面  标的列表 投资fragment
 */
public class TabTenderListLicaiFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2 {

    private String url = Constants.BASEURL + "bmarkApp/bmarkList";

    private View viewRoot;
    private FlowRadioGroup rg_loanlift; //借款期限
    private FlowRadioGroup rg_annual_yield; //年收益
    private FlowRadioGroup rg_tender_progress; //投标进度
    private PullToRefreshListView pullToRefreshListView;

    private TabTenderListLicaiAdapter adapter;
    private List<TenderInfo> list = new ArrayList<>();

    private Map<String, String> conditionMap = new HashMap<>();
    private int page = 1;
    private int pull_flag = 1; //1表示下拉刷新，2表示上拉加载更多，

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (viewRoot == null) {
            viewRoot = getParentView(inflater, container, R.layout.fragment_tab_tender_list_item);
            initView();
            initData();
        }
        return viewRoot;
    }

    @Override
    public void initView() {
        pullToRefreshListView = (PullToRefreshListView) viewRoot.findViewById(R.id.pull_refresh_list);
        View headView = getActivity().getLayoutInflater().inflate(R.layout.fragment_tab_tender_list_item_lc, null);
        pullToRefreshListView.getRefreshableView().addHeaderView(headView);
        rg_loanlift = (FlowRadioGroup) viewRoot.findViewById(R.id.rg_loanlift);
        rg_annual_yield = (FlowRadioGroup) viewRoot.findViewById(R.id.rg_annual_yield);
        rg_tender_progress = (FlowRadioGroup) viewRoot.findViewById(R.id.rg_tender_progress);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, TenderInvestActivity.class);
                intent.putExtra("info", list.get(position - 2));
                startActivity(intent);
            }
        });

        rg_loanlift.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_loanlift_no:
                        conditionMap.remove("termm");
                        conditionMap.remove("terml");
                        break;
                    case R.id.rb_loanlift_three:
                        conditionMap.remove("terml");
                        conditionMap.put("termm", "3");
                        break;
                    case R.id.rb_loanlift_six:
                        conditionMap.put("terml", "3");
                        conditionMap.put("termm", "6");
                        break;
                    case R.id.rb_loanlift_twelve:
                        conditionMap.put("terml", "7");
                        conditionMap.put("termm", "12");
                        break;
                }
                pullToRefreshListView.onRefreshingStart();
            }
        });

        rg_annual_yield.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_annual_yield_no:
                        conditionMap.remove("anyieldl");
                        conditionMap.remove("anyieldm");
                        break;
                    case R.id.rb_annual_yield_10:
                        conditionMap.remove("anyieldl");
                        conditionMap.put("anyieldm", "10");
                        break;
                    case R.id.rb_annual_yield_20:
                        conditionMap.put("anyieldl", "10");
                        conditionMap.put("anyieldm", "20");
                        break;
                    case R.id.rb_annual_yield_20_up:
                        conditionMap.put("anyieldl", "20");
                        conditionMap.remove("anyieldm");
                        break;
                }
                pullToRefreshListView.onRefreshingStart();
            }
        });

        rg_tender_progress.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_tender_progress_no:
                        conditionMap.remove("subschl");
                        conditionMap.remove("subschm");
                        break;
                    case R.id.rb_tender_progress_50:
                        conditionMap.remove("subschl");
                        conditionMap.put("subschm", "50");
                        break;
                    case R.id.rb_tender_progress_80:
                        conditionMap.put("subschl", "50");
                        conditionMap.put("subschm", "80");
                        break;
                    case R.id.rb_tender_progress_80_up:
                        conditionMap.put("subschl", "80");
                        conditionMap.put("subschm", "99.999");
                        break;
                    case R.id.rb_tender_progress_full:
                        conditionMap.put("subschl", "100");
                        conditionMap.remove("subschm");
                        break;
                }
                pullToRefreshListView.onRefreshingStart();
            }
        });
    }

    @Override
    public void initData() {
        adapter = new TabTenderListLicaiAdapter(mContext);
        pullToRefreshListView.setAdapter(adapter);
        presenter = new BasePresenter(requestQueue, this);
    }

    @Override
    public void onResume() {
        super.onResume();
        rg_loanlift.clearCheck();
        rg_annual_yield.clearCheck();
        rg_tender_progress.clearCheck();
        pullToRefreshListView.onRefreshingStart();
    }

    @Override
    public void returnData(String tag, String data) {
        LogUtils.i(TabTenderListLicaiFragment.class, data);
        pullToRefreshListView.onRefreshComplete();
        List<TenderInfo> mList = TenderInfo.parseTenderListData(data);
        if (pull_flag == 1) { //表示下拉刷新
            list.clear();
            list.addAll(mList);
        } else if (pull_flag == 2) { //上拉加载更多
            list.addAll(mList);
        }
        if (mList.size() != 0)
            page++;
        adapter.setData(list);
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        pullToRefreshListView.onRefreshComplete();
    }

    private void getData() {
        conditionMap.put("page", page + "");
        conditionMap.put("stat", "54,62");
        presenter.requestPostData(url, conditionMap);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pull_flag = 1;
        page = 1;
        getData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pull_flag = 2;
        getData();
    }

}
